import { json } from "express";
import encrypt from "./encrypt";

/*
 * @Author: your name
 * @Date: 2021-07-02 15:28:03
 * @LastEditTime: 2021-10-17 19:52:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hbs\libs\tools\Req.ts
 */
const request = require("request");
const fs = require("fs");
const config = require("config-lite")(__dirname);
class Req {
    public static async noticestu(org_name, stu_name) {
        let url = `https://api.heiban.icu/teacher_y/org_kqj_select`;
        try {
            let ans = await Req.get(url, { org_name, stu_name });
            return ans;
        } catch (e) {

        }
    }
    public static get(url, params) {
        return new Promise((resolve, reject) => {
            var uri = url;
            if (params) {
                uri += "?";
            }
            for (var p in params) {
                uri += encodeURI(`${p}=${params[p]}&`);
            };
            uri.substr(0, uri.length - 2);
            console.log(uri);
            request(uri, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    resolve(body);
                } else {
                    reject(error);
                }
            })
        });
    }
    public static post(url, params) {
        params = JSON.stringify(params);
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: "POST",
                body: params
            },
                (error, response, body) => {
                    if (!error && response.statusCode == 200) {
                        resolve(body) // 请求成功的处理逻辑
                    }

                });

        });

    }
    public static postJson(url, params) {
        params = JSON.stringify(params);
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: "POST",
                body: params,
                json: true,
                headers: {
                    "content-type": "application/json",
                }
            },
                (error, response, body) => {
                    if (!error && response.statusCode == 200) {
                        resolve(body) // 请求成功的处理逻辑
                    }

                });

        });

    }
    public static pipe(url, params, i) {
        params = JSON.stringify(params);
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: "POST",
                body: params
            }).pipe(fs.createWriteStream('./up/test/' + i + '.jpg'));

        });
    }
}
class Sms {
    //发送验证码
    public static async send(msg, mobiles, templateid) {
        if (typeof (mobiles) === "object") {
            //  mobiles=JSON.stringify(mobiles);
        };

        const url = `http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg`;
        let data = {
            cpcode: config.unicom_sms.cpcode,
            msg: msg,
            mobiles: mobiles,
            excode: "",
            templateid: templateid,
            sign: ""
        };
        data.sign = encrypt.md5(data.cpcode + data.msg + data.mobiles + data.excode + data.templateid + config.unicom_sms.key);
        let result = await Req.post(url, data);
        return result;
    }
}
export { Req, Sms }